
#include <game_state/running/game_running.hpp>
#include <game_state/menu/menu_game_state.hpp>
#include <game_state/level_select/level_select.hpp>
#include <game_state/pause/pause_game_state.hpp>
#include <game_state/running/playing/game_running_playing.hpp>
#include <level/level.hpp>
#include <input/actions/actions.hpp>

#include <level/messages/level_over.hpp>

game_running_state::game_running_state( std::string const & level_file )
	:		m_level( new level( level_file ) )
{
	m_running_state = game_state_ptr( new game_running_playing_state( m_level ) );
	g_user_input.removeAction( actions::pause );
	g_user_input.assignActionToKey( actions::pause, sf::Key::P );

	m_messenger_connection = g_messenger.add_listener( level_over_message::name( ), boost::bind( &game_running_state::message_callback, this, _1, _2 ) );
}

game_running_state::~game_running_state( )
{
}

game_state_ptr game_running_state::step( float delta_time )
{
	if ( g_user_input.getActionInput( actions::pause ).status == keyPressed )
	{
		return game_state_ptr( new pause_game_state( shared_from_this( ) ) );
	}

	m_running_state = m_running_state->step( delta_time );

	if ( m_running_state == nullptr )
	{
		return game_state_ptr( new level_select_state( ) );
	}
	else
	{
		return shared_from_this( );
	}
}

void game_running_state::draw( )
{
	m_running_state->draw( );
}

void game_running_state::message_callback( std::string const & message_name, message_data_ptr data )
{
	if ( message_name == level_over_message::name( ) )
	{
		//	Save data sometime.

		//auto level_over_data = boost::shared_static_cast< level_over_message const >( data );
		//if ( level_over_data->m_won )
		//{
		//	size_t things_collected = m_level->player( ).things( );
		//	float time_taken = m_level->time_elapsed( );
		//	std::string const & filename = m_level->filename( );
		//}
	}
}
